package com.haoran.dwz.common.handler;

import org.apache.log4j.Logger;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import com.fasterxml.jackson.core.JsonParseException;
import com.haoran.dwz.common.exception.TokenException;
import com.haoran.dwz.common.exception.UsmException;
import com.haoran.dwz.rest.R;


/**
 * 
 */
@ControllerAdvice
@ResponseBody
public class UsmExceptionHandler {

	private static final Logger logger = Logger.getLogger(UsmExceptionHandler.class);
	
	
	@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
	@ExceptionHandler(value = UsmException.class)
	public R handle(UsmException e) {
    	logger.error("系统异常 {}", e);
        return R.failure(e);
	}
	/**
	 * 400 - Bad Request
	 */
	@ResponseStatus(HttpStatus.BAD_REQUEST)
	@ExceptionHandler(HttpMessageNotReadableException.class)
	public R handleHttpMessageNotReadableException(
			HttpMessageNotReadableException e) {
		logger.error("could_not_read_json...", e);
		if(e.getCause() instanceof JsonParseException) {
			return R.failure(e.getCause());
		}
		return R.failure(e);
	}

	/**
	 * 400 - Bad Request
	 */
	@ResponseStatus(HttpStatus.BAD_REQUEST)
	@ExceptionHandler({MethodArgumentNotValidException.class})
	public R handleValidationException(MethodArgumentNotValidException e) {
		logger.error("parameter_validation_exception...", e);
		return R.failure(e);
	}
	
	
	@ResponseStatus(HttpStatus.BAD_REQUEST)
	@ExceptionHandler({JsonParseException.class})
	public R handleJsonParseException(JsonParseException e) {
		logger.error("parameter_validation_exception...", e);
		return R.failure(e);
	}
	/**
	 * 405 - Method Not Allowed。HttpRequestMethodNotSupportedException
	 * 是ServletException的子类,需要Servlet API支持
	 */
	@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
	@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
	public R handleHttpRequestMethodNotSupportedException(
			HttpRequestMethodNotSupportedException e) {
		logger.error("request_method_not_supported...", e);
		return R.failure(e);
	}

	/**
	 * 415 - Unsupported Media Type。HttpMediaTypeNotSupportedException
	 * 是ServletException的子类,需要Servlet API支持
	 */
	@ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
	@ExceptionHandler({ HttpMediaTypeNotSupportedException.class })
	public R handleHttpMediaTypeNotSupportedException(HttpMediaTypeNotSupportedException e) {
		logger.error("content_type_not_supported...", e);
		return R.failure(e);
	}

	/**
	 * 401 - TokenException
	 * 
	 */
	@ResponseStatus(HttpStatus.UNAUTHORIZED)
	@ExceptionHandler(TokenException.class)
	public R handleTokenException(TokenException e) {
		logger.error("Token is invaild...", e);
		return R.failure(e);
	}

	@ResponseStatus(HttpStatus.UNAUTHORIZED)
	@ExceptionHandler(DisabledException.class)
	public R handleDisabledException(DisabledException e) {
		logger.error("Account is invaild...", e);
		return R.failure(e);
	}

	@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
	@ExceptionHandler(Exception.class)
	public R handleDisabledException(Exception e) {
		logger.error("内部错误", e);
		return R.failure(e);
	}
}
